//mutations.js ：保存所有方法，用来改变state的数据


export const  setGoodsSelectorPageVisible =(state, value) =>{
    state.goodsSelectorPageVisible = value;
}
export const  setConHeight =(state, value) =>{
    state.conHeight.height = value;
}

export const setSessionUser =(state, value) =>{
    console.log("setSessionUser ",value)
    state.sessionUser = value;

}

export const setMenuList = (state, data)=>{
    state.menuList=data;
    console.log("setMenuList",data)
}


// 添加tabs
export const addTabs =(state, data)=> {
    console.log("add tab",data)
    state.openTab.push(data);
}
// 更新tabs
export const updateTabs =(state, data)=> {
    console.log("update tab",data)
    for (let item of state.openTab) {
        if (item.path === data.path) {
            item.name=data.name;
        }
    }
}
// 删除tabs
export const deleteTabs =(state, path) =>{
    let index = 0;
    for (let option of state.openTab) {
        if (option.path === path) {
            break;
        }
        index++;
    }
    state.openTab.splice(index, 1);
}

export const setTabs = (state, data)=>{
    state.openTab=data;
    console.log("setTabs",data)
}

export const spliceTabsByIndex =(state, data) =>{
    let start = data.start;
    let end=data.end;
    state.openTab.splice(start, end);
}

// 设置当前激活的菜单
export const setActiveMenu= (state, menu)=>{
    state.activeMenu = menu;
}
//添加micro
export const addMicro =(state, data)=> {
    console.log("add micro",data)
    state.microList.push(data);
}
//更新micro
export const updateMicro =(state, data)=> {
    console.log("update micro",data)
    for (let item of state.microList) {
        if (item.activeRule === data.activeRule) {
            item=data;
        }
    }
}
//删除micro
export const deleteMicro =(state, activeRule) =>{
    console.log("delete micro",activeRule)
    let index = 0;
    for (let item of state.microList) {
        //console.log(item.activeRule)
        if (item.activeRule === activeRule) {
            break;
        }
        index++;
    }
    state.microList.splice(index, 1);
}
//设置当前micro
export const setCurrentMicro= (state, data)=>{
    state.currentMicro = data;
}
//添加可缓存的组件
export const addViewIncludes= (state, data)=>{
    let viewIncludes=  state.viewIncludes ;
    let arr=viewIncludes.split(",");
    let cName=arr.find(item => item===data);
    if (!cName){
        viewIncludes=viewIncludes+data+",";
        state.viewIncludes=viewIncludes;
        console.log("addViewIncludes",data)
    }
}
//删除可缓存的组件
export const deleteViewIncludes= (state, data)=>{
    let viewIncludes=  state.viewIncludes ;
    viewIncludes=viewIncludes.replace(data+",","")
    state.viewIncludes=viewIncludes;
    console.log("deleteViewIncludes",data)
}

export const cleanViewIncludes= (state, data)=>{
    state.viewIncludes=",";
    console.log("cleanViewIncludes")
}



export const setMicroConfigListByApp= (state, appList)=>{
    let microConfigList=[];
    for (let key in appList) {
        let app=appList[key];
        let microConfig=
            {   name: app.appName,
                entry: '//'+app.baseUrl+'/',
                container: '#QK_'+app.id,
                domID: 'QK_'+app.id,
                activeRule: '/'+app.appCode,
                props: {
                    routerBase:'/'+app.appCode,
                    //sessionUser:state.sessionUser,
                }
            }
        microConfigList.push(microConfig);
    }
    console.log("setMicroConfigListByApp",microConfigList)
    state.microConfigList = microConfigList;
}

export const setMicroConfigList= (state, microConfigList)=>{
    console.log("setMicroConfigList",microConfigList)
    state.microConfigList = microConfigList;
}

export const setHomeMenu= (state, data)=>{
    console.log("setHomeMenu",data)
    state.homeMenu = data;
}



export const  initState= (state, data)=>{
    state.currentMicro={};
    state.microList=[];
    state.activeMenu={id:0,menuName:'home',path:'/home',appCode:'',compo:'home',frame:1};
    state.openTab=[];
    state.viewIncludes='';
    state.microConfigList=[];
    state.menuList=[];
    state.sessionUser={};
}



